﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DAL
{
    public class HoaDonDAL
    {
        PhongMachTuDBDataContext db;
        public IQueryable LayDSHoaDon()
        {
            db = new PhongMachTuDBDataContext();
            var q = from s in db.HoaDons
                    select new
                    {
                        s.HoaDonID,
                        s.MaBenhNhan,
                        s.NgayKham,
                        s.TienThuoc,
                        s.TienKham,
                        s.TongTien,
                    };
            return q;
        }
        public IQueryable LayDSHoaDon(int ma)
        {
            db = new PhongMachTuDBDataContext();
            var q = from s in db.HoaDons
                    where s.MaKhamBenh==ma
                    select new
                    {
                        s.HoaDonID,
                        BENHNHAN = s.BENHNHAN.HoTen,
                        s.MaBenhNhan,
                        s.NgayKham,
                        s.TienThuoc,
                        s.TienKham,
                        s.TongTien,
                    };
            return q;
        }
        public int thongKeHoaDonTheoNgay(int day, int month, int year)
        {
            db = new PhongMachTuDBDataContext();
            var q = from s in db.HoaDons
                    where s.NgayKham.Date.Day == day && s.NgayKham.Month == month && s.NgayKham.Year == year
                     select s;
            int doanhThu = 0;
            foreach (var item in q)
            {
                doanhThu += item.TongTien;
            }
            return doanhThu;
        }


        public bool ThemHoaDon(HoaDon r)
        {
            try
            {
                db = new PhongMachTuDBDataContext();
                HoaDon s = new HoaDon();
                s.MaBenhNhan = r.MaBenhNhan;
                s.MaKhamBenh = r.MaKhamBenh;
                s.NgayKham = r.NgayKham;
                s.TienThuoc = r.TienThuoc;
                s.TienKham = r.TienKham;
                s.TongTien = r.TongTien;
                db.HoaDons.InsertOnSubmit(r);
                db.SubmitChanges(); return true;
            }
            catch (Exception)
            {

                return false;
            }


        }
        public bool suaHoaDon(HoaDon s)
        {

            try
            {
                db = new PhongMachTuDBDataContext();
                HoaDon r = db.HoaDons.Single(id => id.HoaDonID == s.HoaDonID);
                s.MaBenhNhan = r.MaBenhNhan; s.MaKhamBenh = r.MaKhamBenh;
                s.NgayKham = r.NgayKham;
                s.TienThuoc = r.TienThuoc;
                s.TienKham = r.TienKham;
                s.TongTien = r.TongTien;
                db.SubmitChanges();
                return true;

            }
            catch (Exception)
            {

                return false;
            }
        }
        public bool xoaHoaDon(int MaHoaDon)
        {
            try
            {
                db = new PhongMachTuDBDataContext();
                var delete = from d in db.HoaDons
                             where d.HoaDonID == MaHoaDon
                             select d;
                if (delete.Count() > 0)
                {
                    db.HoaDons.DeleteOnSubmit(delete.FirstOrDefault());
                    db.SubmitChanges();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
    }
}
